package com.housekeeper.manage.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.housekeeper.api.accessory.service.BillAccessoryService;
import com.housekeeper.api.manage.dtos.bill.*;
import com.housekeeper.api.manage.service.BillService;
import com.housekeeper.api.owner.service.BillOwnerService;
import com.housekeeper.common.exception.HKException;
import com.housekeeper.common.response.ResponseEnum;
import com.housekeeper.common.utils.BeanHelper;
import com.housekeeper.common.utils.DateUtils;
import com.housekeeper.manage.mapper.BillMapper;
import com.housekeeper.manage.mapper.OperationMapper;
import com.housekeeper.manage.pojos.Bill;
import com.housekeeper.manage.pojos.Operation;
import org.apache.dubbo.config.annotation.Reference;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

@Service
@Component
public class BillServiceImpl implements BillService {
    @Autowired
    private BillMapper billMapper;
    @Reference
    private BillAccessoryService accessoryService;
    @Reference
    private BillOwnerService ownerService;
    @Autowired
    private StringRedisTemplate redisTemplate;
    @Autowired
    private OperationMapper operationMapper;

    @Override
    public PageInfo getOwnerBill(OwnerPageListDto ownerPageListDto) {
        PageHelper.startPage(ownerPageListDto.getCurrentPage(),ownerPageListDto.getSize());
        List<OwnerBillDto> ownerBillDtoList = billMapper.getOwnerBill();
        return new PageInfo(ownerBillDtoList);
    }

    @Override
    public void generateBill(OrderBillDto orderBillDto) {
        if (orderBillDto == null){
            throw new HKException(ResponseEnum.INVALID_PARAM_ERROR);
        }
        if (orderBillDto.getType() == 0){
            OrderDetailDto orderDetailDto = accessoryService.getOrderById(orderBillDto.getOrderCode());
            OwnerDto ownerDto = ownerService.getOwnerById(orderDetailDto.getROwnerId());
            Date createDate = DateUtils.stringToDate(orderDetailDto.getCreateTime());
            Bill bill = new Bill();
            bill.setBillCode(redisTemplate.opsForValue().increment("bill_code"));
            bill.setSummary(orderDetailDto.getDescribe() + "*" + orderDetailDto.getSum() + "订单号[" + orderDetailDto.getOrderCode() + "]");
            bill.setType("销售账单");
            bill.setStatus(1);
            bill.setRemarks("无");
            bill.setSubType("销售账单");
            bill.setPCode(ownerDto.getOwnerCode());
            bill.setROwnerName(ownerDto.getOwnerName());
            bill.setRName(orderDetailDto.getSName());
            bill.setCreateTime(createDate);
            bill.setPrice(orderDetailDto.getPrice());
            bill.setOrderId(orderDetailDto.getOrderId());
            bill.setPName(orderDetailDto.getRName());
            bill.setPAddress(orderDetailDto.getRAddress());
            bill.setPOwnerName(orderDetailDto.getROwnerName());
            bill.setPOwnerTel(orderDetailDto.getROwnerTel());
            bill.setRCode(orderDetailDto.getRCode());
            bill.setRAddress(orderDetailDto.getSAddress());
            bill.setROwnerTel(orderDetailDto.getSTel());
            Operation operation = new Operation();
            operation.setTime(new Date());
            operation.setType("创建账单");
            operation.setDescribe("初始化账单");
            operation.setStatus("待支付");
            operation.setOperator("system");
            operation.setCodeId(bill.getBillCode());
            operation.setIsRecord(1);
            billMapper.insertBill(bill);
            operationMapper.insertOperation(operation);
        }
    }

    @Override
    public OwnerBillDetailDto getOwnerBillDetail(Long billCode) {
        if (billCode == null || billCode == 0){
            throw new HKException(ResponseEnum.INVALID_PARAM_ERROR);
        }
        Bill bill = billMapper.getOwnerBillDetail(billCode);
        Operation operation = operationMapper.getOperation(billCode);
        OwnerBillDetailDto ownerBillDetailDto = BeanHelper.copyProperties(bill, OwnerBillDetailDto.class);
        ownerBillDetailDto.setTime(operation.getTime());
        ownerBillDetailDto.setType(operation.getType());
        ownerBillDetailDto.setDescribe(operation.getDescribe());
        ownerBillDetailDto.setStatus(operation.getStatus());
        ownerBillDetailDto.setOperator(operation.getOperator());
        return ownerBillDetailDto;
    }
}
